/**
 * 
 * @Project le cOeur - Data Virtualization
 * @Date 08.12.2012
 * @Author Aebi Pascal, Kocher Marc
 * @School BFH-TI Biel, Bachelor Thesis
 *
 */

package main.implementation;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.ParserConfigurationException;

import main.framework.HTTPServer;
import main.framework.connection.Protocol;
import main.framework.exception.PortListenerException;
import main.framework.exception.ResourceManagerException;
import main.framework.exception.RootElementNotFoundException;
import main.framework.exception.ServerStatusException;
import main.framework.http.ContentType;
import main.framework.log.LoggerCreator;
import main.framework.security.WhiteList;
import main.framework.setting.Config;
import main.implementation.cache.LeCoeurCacheStrategy;
import main.implementation.representation.HTMLRepresentationCreator;
import main.implementation.representation.JSONRepresentationCreator;
import main.implementation.representation.XMLRepresentationCreator;

import org.xml.sax.SAXException;

public class Server {

	/**
	 * @param args
	 */
	public static void main(String[] args){
		// Logging 
		LoggerCreator systemLogger = new LoggerCreator();
		Logger logger = null;
		try {
			logger = systemLogger.createLogger("SysLogger", Level.INFO, "server.log");
		} catch (SecurityException e) {
			e.printStackTrace();
			System.exit(1);
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
		
		HTTPServer httpServer = null;
		try {
			httpServer = new HTTPServer();
		} catch (KeyStoreException e) {
			logger.severe(e.getMessage());
			System.exit(1);
		} catch (NoSuchAlgorithmException e) {
			logger.severe(e.getMessage());
			System.exit(1);
		} catch (URISyntaxException e) {
			logger.severe(e.getMessage());
			System.exit(1);
		}
		
		// Caching
		httpServer.setCacheStrategy(new LeCoeurCacheStrategy());
		
		// Security
		WhiteList whiteList = new WhiteList();
		whiteList.addHost("vmnorma.bfh.ch");
		whiteList.addHost("147.87.98.149");
		httpServer.setServerAccess(whiteList);
		
		// Ports
		try {
			httpServer.addPortListener(Protocol.TLS, 61837);
		} catch (PortListenerException e) {
			logger.warning(e.getMessage());
		}
		
		// Resources
		try {
			httpServer.createResources(Config.RESOURCE_XML_FILE_PATH);
		} catch (InstantiationException e) {
			logger.severe(e.getMessage());
			System.exit(1);
		} catch (IllegalAccessException e) {
			logger.severe(e.getMessage());
			System.exit(1);
		} catch (ClassNotFoundException e) {
			logger.severe(e.getMessage());
			System.exit(1);
		} catch (ParserConfigurationException e) {
			logger.severe(e.getMessage());
			System.exit(1);
		} catch (SAXException e) {
			logger.severe(e.getMessage());
			System.exit(1);
		} catch (IOException e) {
			logger.severe(e.getMessage());
			System.exit(1);
		} catch (RootElementNotFoundException e) {
			logger.severe(e.getMessage());
			System.exit(1);
		} catch (URISyntaxException e) {
			logger.severe(e.getMessage());
			System.exit(1);
		}
		
		// Representations
		httpServer.addRepresentationCreator(new JSONRepresentationCreator(new ContentType("application/json")), true);
		httpServer.addRepresentationCreator(new XMLRepresentationCreator(new ContentType("application/xml")), false);
		httpServer.addRepresentationCreator(new HTMLRepresentationCreator(new ContentType("text/html")), false);
				
		
		// Initialize the HTTP server
		try {
			httpServer.init();
		} catch (UnrecoverableKeyException e) {
			logger.severe(e.getMessage());
			System.exit(1);
		} catch (NumberFormatException e) {
			logger.severe(e.getMessage());
			System.exit(1);
		} catch (KeyStoreException e) {
			logger.severe(e.getMessage());
			System.exit(1);
		} catch (NoSuchAlgorithmException e) {
			logger.severe(e.getMessage());
			System.exit(1);
		} catch (CertificateException e) {
			logger.severe(e.getMessage());
			System.exit(1);
		} catch (FileNotFoundException e) {
			logger.severe(e.getMessage());
			System.exit(1);
		} catch (IOException e) {
			logger.severe(e.getMessage());
			System.exit(1);
		} catch (PortListenerException e) {
			logger.warning(e.getMessage());
		}
		
		// Start the HTTP server
		try {
			httpServer.start();
		} catch (KeyManagementException e) {
			logger.severe(e.getMessage());
			System.exit(1);
		} catch (NoSuchAlgorithmException e) {
			logger.severe(e.getMessage());
			System.exit(1);
		} catch (ServerStatusException e) {
			logger.warning(e.getMessage());
		} catch (PortListenerException e) {
			logger.warning(e.getMessage());
		} catch (IOException e) {
			logger.severe(e.getMessage());
			System.exit(1);
		} catch (ResourceManagerException e) {
			logger.severe(e.getMessage());
			System.exit(1);
		}
	}

}
